package com.taiji.dicom4cloud.smart.handler;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.netsdk.demo.module.LoginModule;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;
import com.netsdk.utils.CapturePictureUtil;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.taiji.dicom4cloud.smart.model.entity.Camera;
import com.taiji.dicom4cloud.smart.model.entity.RfidRecord;
import com.taiji.dicom4cloud.smart.rfid.config.MqttGatewayService;
import com.taiji.dicom4cloud.smart.service.AlarmRecordService;
import com.taiji.dicom4cloud.smart.service.CameraEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@Component
public class AlaramMessageCB implements NetSDKLib.fMessCallBack{

    @Autowired
    private AlarmRecordService alarmRecordService;

    @Autowired
    private MqttGatewayService mqttGatewayService;

    @Autowired
    private CameraEquipmentService cameraEquipmentService;

    private String deviceId;

    private NetSDKLib.LLong m_hLoginHandle = new NetSDKLib.LLong(0);

    public void setDeviceId(String deviceId, NetSDKLib.LLong m_hLoginHandle){
        this.deviceId = deviceId;
        this.m_hLoginHandle = m_hLoginHandle;
    }

    @Override
    public boolean invoke(int lCommand, NetSDKLib.LLong lLoginID, Pointer pStuEvent, int dwBufLen,
                          String strDeviceIP, NativeLong nDevicePort, Pointer dwUser) {
        switch (lCommand) {
            case NetSDKLib.NET_SHELTER_ALARM_EX://视频遮挡报警
                capturePicture(0,0,0);
                saveCameraData(deviceId,"videoMotion");//NetSDKLib.NET_SHELTER_ALARM_EX
                log.info("视频遮挡报警");
                break;
            case NetSDKLib.EVENT_IVS_PARKINGDETECTION://非法停车事件
                log.info("非法停车报警");
                break;
            case NetSDKLib.NET_ALARM_ALARM_EX://外部报警
                System.out.println("视频2");
                break;
            case NetSDKLib.NET_MOTION_ALARM_EX://动态检测报警
                System.out.println("视频3");

                break;
            case NetSDKLib.NET_VIDEOLOST_ALARM_EX://视频丢失报警
                System.out.println("视频4");
                break;
            case NetSDKLib.NET_DISKFULL_ALARM_EX://硬盘满报警
                System.out.println("视频5");
                break;
            case NetSDKLib.NET_DISKERROR_ALARM_EX: {
                System.out.println("视频6");
                byte []alarm = new byte[dwBufLen];
                pStuEvent.read(0, alarm, 0, dwBufLen);
                for (int i = 0; i < dwBufLen; i++) {
                    if (alarm[i] == 1) {

                    }else {

                    }
                }
                break;
            }
            default:
                break;
        }
        return false;
    }

    //抓图
    private void capturePicture(int chn, int mode, int interval){
        NetSDKLib.SNAP_PARAMS stuSnapParams = new NetSDKLib.SNAP_PARAMS();
        stuSnapParams.Channel = chn;            // channel
        stuSnapParams.mode = mode;                // capture picture mode
        stuSnapParams.Quality = 3;                // picture quality
        stuSnapParams.InterSnap = interval;    // timer capture picture time interval
        stuSnapParams.CmdSerial = 0;            // request serial

        IntByReference reserved = new IntByReference(0);
        if (!LoginModule.netsdk.CLIENT_SnapPictureEx(m_hLoginHandle, stuSnapParams, reserved)) {
            log.error("抓图失败" + ToolKits.getErrorCodePrint());
        } else {
            log.info("抓图成功");
        }
    }

    //数据入库
    public Boolean saveCameraData(String deviceId, String messageType) {
        if(ObjectUtil.isNotNull(deviceId)){
            RfidRecord record = new RfidRecord();
            Camera camera = cameraEquipmentService.getOne(Wrappers.<Camera>lambdaQuery().eq(Camera::getDeviceId,deviceId));
            if(ObjectUtil.isNotNull(camera)){
                record.setOrgName(camera.getOrgName());
                record.setOrgId(camera.getOrgId());
                record.setLatitude(camera.getLatitude());
                record.setLongitude(camera.getLongitude());
                record.setEquipmentName(camera.getCameraEquipmentName());
                record.setEquipmentId(deviceId);
            }
            record.setAlarmTime(LocalDateTime.now());
            record.setAlarmName(messageType+"");
            record.setCameraMessageId("");
            record.setType("2");
            record.setCreateTime(LocalDateTime.now());
            alarmRecordService.save(record);
            mqttGatewayService.send2Mqtt("alarm.rfid", JSONObject.toJSONString(record));
        }
        return Boolean.TRUE;
    }
}
